﻿var dataObjects;
$(function () {
    $('#tt').datagrid({
        loadMsg: "正在接收数据...",
        iconCls: 'icon-reload',
        url: '',
        idField: 'StockBarcodeID',
        rownumbers: true,
        singleSelect: false,
        pagination: true,
        autoRowHeight: false,
        fitColumns: false,
        striped: true,
        pageSize: 20,
        columns: [[
            { field: 'StockBarcodeID', checkbox: true },
            { field: 'BarcodeNO', title: '条码编号' },
            { field: 'MaterialNO', title: '物料编号' },
            { field: 'MaterialName', title: '物料名称' },
            { field: 'MaterialDesc', title: '物料规格' },
            { field: 'WarehouseName', title: '仓库名称' },
            { field: 'StorageNO', title: '储位编号', width: 100 },
            { field: 'LampNO', title: '灯珠编号', width: 100 },
            { field: 'BatchNO', title: '物料批号' },
            { field: 'StockQty', title: '库存数量', width: 100, sortable: true },
            { field: 'PartnerName', title: '供应商名称' },
            { field: 'IsLeadName', title: '含铅' },
            { field: 'IsTinningName', title: '搪锡' },
            { field: 'CreateTime', title: '入库时间', sortable: true },
            { field: 'IsLock', hidden: true },
        ]],
        rowStyler: function (index, row) {
            if (row.IsLock === 1) {
                // 锁定状态（RState=1）：
                return 'background-color:#FF8C00';
            }
        },
        onSortColumn: function (sort, order) {
            $("#hSort").val(sort);
            $("#hOrder").val(order);
            loadLocal();
        }
    });
    $(window).resize(function () {
        $('#tt').datagrid('resize');
    });

    $('#Mtt').datagrid({
        loadMsg: "正在接收数据...",
        iconCls: 'icon-reload',
        url: '',
        idField: 'StockMaterialID',
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        autoRowHeight: false,
        fitColumns: false,
        striped: true,
        PageSize: 20,
        columns: [[
            { field: 'StockMaterialID', hidden: true },
            { field: 'MaterialNO', title: '物料编号', width: 150 },
            { field: 'TradeDay', title: '未异动天数' },
            { field: 'MaterialName', title: '物料名称', width: 150 },
            { field: 'MaterialDesc', title: '物料规格' },
            { field: 'WarehouseName', title: '仓库名称', width: 80 },
            { field: 'StorageNO', title: '储位编号', width: 50 },
            { field: 'BatchNO', title: '物料批号' },
            { field: 'StockQty', title: '库存数量', width: 50, sortable: true },
            { field: 'CreateTime', title: '入库时间', sortable: true }
        ]],
        onSortColumn: function (sort, order) {
            $("#hSort").val(sort);
            $("#hOrder").val(order);
            MloadLocal();
        }
    });
    $(window).resize(function () {
        $('#Mtt').datagrid('resize');
    });

    // 下拉框加载 - 调出仓库
    $('#tFromWarehouseID').combobox({
        url: '/api/BAS/GetWarehouseInfo',
        method: 'POST',
        valueField: 'WarehouseID',
        textField: 'WarehouseName',
        panelHeight: 'auto',
        editable: false,
        required: false,
        onLoadSuccess: function (data) {
            $('#tFromWarehouseID').combobox('setValue', data[0].WarehouseID);
        },
        onLoadError: function (error) {
            console.error('调出仓库加载失败:', error);
        },
        onChange: function (newVal) {
            $('#hFromWarehouseID').val(newVal); // 更新隐藏字段
        }
    });
    $("#stockTabs").tabs({
        onSelect: function (title, index) {
            if (index === 0) { // 第一个标签页
                loadLocal();
            } else if (index === 1) { // 第二个标签页
                MloadLocal(); // 需要创建这个函数
            }
        }
    });
})
function MloadLocal() {
    var StorageNO = $("#txtStorageNO").val();
    var MaterialNO = $("#txtMaterialNO").val();
    var BarcodeNO = $("#txtBarcodeNO").val();
    var WarehouseID = $("#tFromWarehouseID").val();
    var sort = $("#hSort").val();
    var order = $("#hOrder").val();
    var BeginTime = $("#BeginTime").val();
    var EndTime = $("#EndTime").val();

    $("#Mtt").datagrid({
        url: '/api/WMS/QueryWmsStockMaterInfo',
        method: 'Get',
        queryParams: {
            sort: sort, order: order,
            StorageNO: StorageNO,
            MaterialNO: MaterialNO,
            BarcodeNO: BarcodeNO,
            BeginTime: BeginTime, EndTime: EndTime,
            WarehouseID: WarehouseID
        },
        loadFilter: function (data) {
            return JSON.parse(data);
        }
    });
    $('#Mtt').datagrid('clearSelections');
}
function loadLocal() {
    /*  var WarehouseNO = $("#txtWarehouseNO").val();//编号*/
    var StorageNO = $("#txtStorageNO").val();
    var MaterialNO = $("#txtMaterialNO").val();
    var BarcodeNO = $("#txtBarcodeNO").val();
    var WarehouseID = $("#tFromWarehouseID").val();
    var sort = $("#hSort").val();
    var order = $("#hOrder").val();
    var BeginTime = $("#BeginTime").val();
    var EndTime = $("#EndTime").val();
    var IsLock = $("#tIsLock").val();
    $("#tt").datagrid({
        url: '/api/WMS/QueryWmsStockBarCode',
        method: 'Get',
        queryParams: {
            sort: sort, order: order,
            StorageNO: StorageNO,
            MaterialNO: MaterialNO,
            BarcodeNO: BarcodeNO,
            BeginTime: BeginTime, EndTime: EndTime,
            WarehouseID: WarehouseID, IsLock: IsLock
        },
        loadFilter: function (data) {
            return JSON.parse(data);
        }
    });
    $('#tt').datagrid('clearSelections');
    //$("#Mtt").datagrid({
    //    url: '/api/WMS/QueryWmsStockMaterInfo',
    //    method: 'Get',
    //    queryParams: {
    //        sort: sort, order: order,
    //        StorageNO: StorageNO,
    //        MaterialNO: MaterialNO,
    //        BarcodeNO: BarcodeNO,
    //        BeginTime: BeginTime, EndTime: EndTime,
    //        WarehouseID: WarehouseID
    //    },
    //    loadFilter: function (data) {
    //        return JSON.parse(data);
    //    }
    //});
    //$('#Mtt').datagrid('clearSelections');
    //$(window).resize(function () {
    //    $('#tt').datagrid('resize');
    //    $('#Mtt').datagrid('resize');
    //});
};

//解除锁定状态
function IsLock() {
    var rows = $('#tt').datagrid('getSelections');

    if (rows.length < 1) {
        Alert("请选择！");
        return false;
    }
    Confirm("确定要解除" + rows.length + "个条码锁定吗？", function () {
        var MateialInfo = fnSelected(rows, $("#tt").datagrid('options').columns[0]);
        var url = '/api/WMS/RelieveIsLock';
        DataService.PostJson(url, MateialInfo, function (error, response) {
            if (error) {
                Notice("Success!");
            } else {
                Alert("Error!");
            }
            loadLocal();
        })
    });
};

function myformatter(date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
}
function myparser(s) {
    if (!s) return new Date();
    var ss = (s.split('-'));
    var y = parseInt(ss[0], 10);
    var m = parseInt(ss[1], 10);
    var d = parseInt(ss[2], 10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)) {
        return new Date(y, m - 1, d);
    } else {
        return new Date();
    }
}
//导出
function DToExcel() {
    /*  var WarehouseNO = $("#txtWarehouseNO").val();//编号*/
    var StorageNO = $("#txtStorageNO").val();
    var MaterialNO = $("#txtMaterialNO").val();
    var BarcodeNO = $("#txtBarcodeNO").val();
    var sort = $("#hSort").val();
    var order = $("#hOrder").val();
    var BeginTime = $("#BeginTime").val();
    var EndTime = $("#EndTime").val();
    DataService.AjaxPost('/api/WMS/GetAllStockBarCode', {
        StorageNO: StorageNO,
        MaterialNO: MaterialNO,
        BarcodeNO: BarcodeNO,
        BeginTime: BeginTime, EndTime, EndTime,
    }, function (err, res) {
        if (err) {
            console.log(err);
            ToExcel('tt', JSON.parse(err));
        }
    })
}